package solution;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-04-06
 * Time: 17:30
 */
//https://leetcode.cn/problems/perfect-squares/description/
class Solution4 {
    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            int minn = Integer.MAX_VALUE;
            for (int j = 1; j * j <= i; j++) {//i 减去最大的j*j(小于i)后，minn选出最小的剩余
                minn = Math.min(minn, dp[i - j * j]);
            }
            //1 即为j*j的数
            dp[i] = minn + 1;
        }
        return dp[n];
    }
}